**Author: Abeberese, Barnwal, Chaurey, Mukherjee
**This do-file will generate Table A4
**Date: May 2021


clear
capture log close
set matsize 4000
set scheme s2mono, perm

**Set directories
*Add personal root path for your machine directory

global ROOT "/Users/rchaurey/Dropbox/Indonesia Firms/DataFolderRESTAT/"
global dofiles "${ROOT}/Code"
global data "${ROOT}/Input"
global output "${ROOT}/Output/"


****************************************************************************************************************************************************
*START ANALYSIS
use "${data}/mayor_manufactureBaselineRegressions_longrun", clear

sort idno year
xtset idno year


drop minyear
bys idno: egen minyear = min(year)
bys idno: egen maxyear = max(year)


gen entry = year == minyear if !missing(year)
gen exit = year == maxyear if !missing(year)

*Keep districts

sort district
bys district year: gen n = _n==1
bys district: egen sn = sum(n)
drop n
tab sn
keep if sn==20



*District-level regressions for entry and exit
gen firstdemyr = mayor_appyr+5
gen post = year>=firstdemyr
replace post = . if mayor_appyr==.

gen num = 1

collapse (sum) num (sum) entry (sum) exit   (max)  post, by( district year)
replace entry = . if year==1993 /*dataset goes from 1993 to 2014*/
replace exit=. if year==2014 /*dataset goes from 1993 to 2014*/

gen entryrate = entry/num
gen exitrate = exit/num


label variable entryrate "firm entry rate"
label variable exitrate "firm exit rate"



foreach v of varlist  entryrate exitrate{
reghdfe `v'  post ,   absorb(year district )  cluster(district)
outreg2 using "${output}A4.xls", ///
keep(post)   addtext(district FE, Yes, year FE, Yes) append

}

